/*
 * global js
 * adv,
 * author:buryram
 */
(function ($) {
	/*
	 * slider list
	 * @param _obj:target
	 * @param _mask:loading
	 * @param _fn:
	 */
	var picPath;
	$.LoadAjax = function (arg) {
		var $defaults = {
			dataType : "json",
			mask : '<div id="loading">正在加载中...</div>',
			fn : function(obj,data){
					for(var i in data) {		
						$(obj).append('<li data-appId="'+data[i].appId+'" data-id="'+data[i].id+'"><dl><dd><img src="'+requestPicPath+data[i].picPath+'" /></dd><dt><strong>'+data[i].name+'</strong><span>大小：'+data[i].size+'</em></span></dt><dd><button>推荐</button><button>下载</button></dd></dl></li>');
					}					
			},
			async : true,
			tab : 0,
			appName : '',
			modelId : 0,
			pagenow : 1
		};
		arg = $.extend($defaults,arg);
		
		var _obj_renderTarget = arg.renderTarget;
		var _mask = arg.mask;
		var _dataType = arg.dataType;
		var _fn = arg.fn;
		var _url = arg.url;
		var _srcollingWrap = arg.srcollingWrap;
		var _async = arg.async;
		var _pageSize = arg.pageSize;
		var _appType = arg.appType;
		var _tab = arg.tab;
		var _appName = arg.appName;
		var _modelId = arg.modelId;
		var _pagenow = arg.pagenow;
		var _srcollingWrap = arg.srcollingWrap;
		picPath = picPath==null?arg.picPath:picPath;
		var loadData = function(obj,mask,dataType,fn,url,pageSize,appType,tab,appName,modelId,pagenow,async){
			$(obj).ajaxStart(function(){
				$(this).next().show();
			});
			$.ajax({	
				type : "get",
				dataType : dataType,
				headers:{"Accept":"application/json"},
				data:{
					pageSize:pageSize,
					appType:appType,
					tab:tab,
					appName:appName,
					modelId:modelId,
					offset:pageSize*(pagenow-1),
				},
				async:async,
				url : url,
				success : function(data) {
					fn(obj,data,mask,appName);
				},
				error : function(XMLHttpRequest, textStatus, errorThrown){
					$(mask).remove();
					return;
				},
			});
			$(obj).ajaxComplete(function(){
				$(this).next().hide();
			});
		};

		// 滚动加载
		this.scrollLoading = function(){		
			$(_srcollingWrap).bind("scroll", function(){			 
				var wrap = $(this);
				var wh=$(window).height(); 
				var sct=$(document).scrollTop();
				var w=$(document).height();
				if(w<=sct+wh){
					$(_obj_renderTarget).next().fadeIn(100);
					_pagenow++;
					setTimeout(loadData(_obj_renderTarget,_mask,_dataType,_fn,_url,_pageSize,_appType,_tab,_appName,_modelId,_pagenow,_async),5000);
					return false;
				}
			});
		};

		/*this.loadAppListPager = function(){
			this.loadAppList();
			//$(_obj_renderTarget).append(_mask_srcolling);
			scrollLoading(_srcollingWrap,_obj_renderTarget);
		};*/
		
		var eventSupported = function( eventName,el ) {
			el = el || document.createElement("div");
			eventName = "on" + eventName;
			var isSupported = (eventName in el);
			if (el.setAttribute && !isSupported ) {
				el.setAttribute(eventName, "return;");
				isSupported = typeof el[eventName] === "function";
			}
			el = null;
			return isSupported;
		};
			
		var addEvent = function(obj,type,callback){
			if ( obj.addEventListener ) {
				obj.addEventListener( type, callback, false );
			} else if ( obj.attachEvent ) {
				obj.attachEvent( "on" + type, callback );
			}
		};
		
		var type = eventSupported("mousewheel") ? "mousewheel":"DOMMouseScroll";
		//IE下window没有滚轮事件
		var wheel = function(obj,callback){
			addEvent(obj, type,function(event){
			event = event || window.event;
			var delta = 0;
			if (event.wheelDelta) {
				delta = event.wheelDelta/120;
			  //opera 9x系列的滚动方向与IE保持一致，10后修正
				if(window.opera && window.opera.version() < 10)
					delta = -delta;
				} else if (event.detail) {
					delta = -event.detail/3;
				}
				//由于事件对象的原有属性是只读，我们只能通过添加一个私有属性delta来解决兼容问题
				event.delta =  Math.round(delta); //修正safari的浮点 bug
				callback.call(obj,event);
			});
		};
		
		this.turnPage = function(){
			//alert(1);
			_pagenow++;
			this.loadAppList();
			wheel(document,function(e){
				$(".wrap #appList").stop(true,false);
			});
		};
		
		this.loadAppList = function(){
			//alert(_obj_renderTarget+','+_mask+','+_fn+','+_url+','+_pageSize+','+_tab+','+_appName+','+_modelId+','+_pagenow+','+_async);
			loadData(_obj_renderTarget,_mask,_dataType,_fn,_url,_pageSize,_appType,_tab,_appName,_modelId,_pagenow,_async);
		};
		
		this.init = function(){
			$(_obj_renderTarget).after(_mask);
			//this.scrollLoading();
		};
	};
	
	
	/*
	 * global
	 * @param _obj:adv
	 * @param _fn:hover
	 * @param _index:ranking
	 */
	$.Switchover = function (arg) {
		var $defaults = {
			obj : ".list ul li dl dt,.list ul li dl dd:first-child",
			fn : function(obj){
				obj.parents("li").css("background","#efefef").siblings().css("background","none");
//				window.location.href = "detail.html?appId="+obj.parents("li").attr("data-appId")+"&id="+obj.parents("li").attr("data-id");
				window.proxy.toDetail("detail.html?appId="+obj.parents("li").attr("data-appId")+"&id="+obj.parents("li").attr("data-id"));
			},
			index : null
		};
		arg = $.extend($defaults,arg);
		
		var _obj = arg.obj;
		var _fn = arg.fn;
		var _index = arg.index;

	
		// 菜单tab
		var switchover = function(obj){
			$(obj).live("click",function(){
				$(this).addClass("current").siblings(".current").removeClass();
				fn($(this));
			});
		};
		
		var fn = _fn;
		
		// load列表
		this.getIndex = function(){
			/*var index = 0;
			$(_obj).parents(".tree").find("dt").each(function(i){
				
				if($(this).hasClass("current")){
					index = i;
					return true;
				}
            });
			alert(index);*/
			return $(_obj).parents(".tree").find("dt.current").index();
			//return index;
		};

		// 初始化
		this.init = function(){
			switchover(_obj);
			_index==null?null:$(_obj+":eq("+_index+")").addClass("current").next("dd").slideDown();
			/*$(_obj).parents("dl").ajaxComplete(function(event,request, settings){
				$(_obj+":eq("+_index+")").addClass("current").next("dd").slideDown();
			});*/
		};
	};
	
	
	/*
	 * search
	 * @param obj_txt:adv
	 * @param obj_btn:hover
	 * @param obj_tag:ranking
	 */
	$.Searcher = function (arg) {
		var $defaults = {
			obj_txt : ".wrap #search input[type=text]",
			obj_btn : ".wrap #search input[type=submit]",
			obj_tag : ".wrap #search-tag ul li a",
		};
		arg = $.extend($defaults,arg);
		
		var _obj_txt = arg.obj_txt;
		var _obj_btn = arg.obj_btn;
		var _obj_tag = arg.obj_tag;
		var _fn = arg.fn;	
	
		// 菜单tab
		var searcher = function(obj_txt,obj_btn,obj_tag){
			var txt,filters=["_","%","^"];
			$(obj_txt).attr("placeholder",$(obj_tag).eq(Math.round(Math.random()*$(obj_tag).length-1)).text());
			$(obj_btn).live("click",function(){
				txt = $(obj_txt).val()==""?$(obj_txt).attr("placeholder"):$(obj_txt).val();
				if(txt!=""&&txt==$(obj_txt).parent().siblings(".list").find("span:first").text()){
					return;
				}
				fn(txt,true,true);
			});
			$(obj_tag).live("click",function(){
				txt = $(this).text();
				fn(txt,true,true);
			});
			$(obj_txt).parent().siblings(".list").find("a").click(function(){
				slideTab();
			});
			txt = $(obj_txt).attr("placeholder")
			$(obj_txt).attr("placeholder",txt==""?$(obj_tag).eq(0).text():txt);
		};
		
		var fn = _fn;
		
		// 初始化
		this.init = function(){
			searcher(_obj_txt,_obj_btn,_obj_tag);
		};
	};
	
	/*
	 * download
	 * @param obj_txt:adv
	 * @param obj_btn:hover
	 * @param obj_tag:ranking
	 */
	$.Download = function (arg) {
		var _obj = arg.obj;
	
		// 应用下载
		var downloadApp = function(obj){
			$(obj).live("click",function(){
				if($(this).parents("li")[0]){
					name = $(this).parent().siblings("dt").find("strong").text();
					icon = $(this).parent().siblings("dd:first").find("img").attr("src");
					//appId = $(this).parents("li").attr("data-appId");
					url = $(this).attr("data-url");
				}
				//alert(name+","+icon+","+url);
				window.proxy.download(name,icon,url);
				/*$.ajax({	
					type : "get",
					dataType : "xml",
					headers:{"Accept":"application/json"},
					url : requestPath+"/websamp/appInstall/getXmlForInstall?operSystem=51&appParam=T:"+appId,
					success : function(data) {
						$(data).find("AppInstall").find("Apps").each(function(){
							url = $(this).find("url").text();
							window.proxy.download(icon, url);
						});
					}
				});*/
			});
		};
		
		// 初始化
		this.init = function(){
			downloadApp(_obj);
		};
	};
})(jQuery);
//var requestPath = "http://www.phone580.com:8082";
var requestPath = "http://221.179.6.75:8082";
//var requestPath = "http://192.168.0.31:8089";
var requestPicPath = requestPath+"/xfolder";
var name,icon,url,flag,appTypes = ["APP","GAME"];
var slideTab= function(){
	$("#appList").slideToggle().prev().slideToggle();
};
var getUrlParam = function (param){
	var reg = new RegExp("(^|&)"+ param +"=([^&]*)(&|$)");  
	var r = window.location.search.substr(1).match(reg);
	return r==null?"":decodeURI(r[2]);
};